<?php

require_once(FWS_DIR_LIB.'core/ModelAndView.class.php');
require_once(FWS_DIR_MODEL_DAO.'competitionmodeldao.class.php');
require_once(FWS_DIR_MODEL.'competitionmodel.class.php');
require_once(FWS_DIR_MODEL_DAO.'teammodeldao.class.php');
require_once(FWS_DIR_MODEL.'teammodel.class.php');
require_once(FWS_DIR_MODEL_DAO.'matchmodeldao.class.php');
require_once(FWS_DIR_MODEL.'matchmodel.class.php');
require_once(FWS_DIR_MODEL_DAO.'rankingmodeldao.class.php');
require_once(FWS_DIR_MODEL.'rankingmodel.class.php');

class CompetitionMultiActionController {

	var $_competitionmodeldao;
	var $_teammodeldao;
	var $_matchmodeldao;
	var $_rankingmodeldao;

	function CompetitionMultiActionController() {
		$this->_competitionmodeldao = new CompetitionModelDao();
		$this->_teammodeldao = new TeamModelDao();
		$this->_matchmodeldao = new MatchModelDao();
        $this->_rankingmodeldao = new RankingModelDao();
		mysql_connect(FWS_DBDOMAIN,FWS_DBUSER,FWS_DBPASS) or exit(mysql_error());
		mysql_select_db(FWS_DBNAME) or exit(mysql_error());		
	}

	function list_action() {
		$mv = new ModelAndView();
		$mv->addToModel(0, $this->_competitionmodeldao->findAllCompetition());
		mysql_close();		
		$mv->setView('list_competition.tpl');
		return $mv;
	}

	function profile_action() {
		$mv = new ModelAndView();
		$mv->addToModel(0, $this->_competitionmodeldao->queryCompetitionById($_GET[GLOBAL_PARAM_IDCOMPETITION]));
		$mv->addToModel(1, $this->_competitionmodeldao->getTeams($_GET[GLOBAL_PARAM_IDCOMPETITION]));
		mysql_close();		
		$mv->setView('profile_competition.tpl');
		return $mv;
	}

	function addteamForm_action() {
		$mv = new ModelAndView();
		$mv->addToModel(0, $this->_competitionmodeldao->queryCompetitionById($_GET[GLOBAL_PARAM_IDCOMPETITION]));
		$mv->addToModel(1, $this->_competitionmodeldao->getTeams($_GET[GLOBAL_PARAM_IDCOMPETITION]));
		$mv->addToModel(2, $this->_teammodeldao->findRefTeam());
		mysql_close();		
		$mv->setView('competition_addteamform.tpl');
		return $mv;
	}

	function genmatch_action() {
		$mv = new ModelAndView();
		$mv->addToModel(0, $this->_competitionmodeldao->GenererChampionnat($_GET[GLOBAL_PARAM_IDCOMPETITION]));
		$mv->addToModel(1, $this->_matchmodeldao->queryMatchByCompet($_GET[GLOBAL_PARAM_IDCOMPETITION]));
		mysql_close();		
		$mv->setView('list_match.tpl');
		return $mv;
	}
	
	function updateForm_action() {
		$mv = new ModelAndView();
		$mv->addToModel(0, $this->_competitionmodeldao->queryCompetitionById($_GET[GLOBAL_PARAM_IDCOMPETITION]));
		mysql_close();		
		$mv->setView('competition_updateform.tpl');
		return $mv;
	}
	
	function update_action() {
		$mv = new ModelAndView();
		$this->_competitionmodeldao->updateCompetition(new CompetitionModel($_POST)); 
		
		return $this->list_action();
	}
	
	function insertForm_action() {
		$mv = new ModelAndView();
		mysql_close();		
		$mv->setView('competition_insertform.tpl');
		return $mv;
	}

	function insert_action() {
		$mv = new ModelAndView();
		$this->_competitionmodeldao->insertCompetition(new CompetitionModel($_POST)); 
				
		return $this->list_action();
	}

	function addteam_action() {
		$mv = new ModelAndView();
		$this->_competitionmodeldao->addTeam(new CompetitionModel($_POST)); 
		
		return $this->list_action();		
	}
	
    function fillresultform_action() {
		$mv = new ModelAndView();
		$mv->addToModel(0, $this->_competitionmodeldao->findAllChamp());

		if(isset($_POST[GLOBAL_PARAM_IDCOMPETITION])) {
                        
			$mv->addToModel(1, $this->_matchmodeldao->queryMatchByCompet($_POST[GLOBAL_PARAM_IDCOMPETITION]));
		}
        $mv->addToModel(2, $_POST[GLOBAL_PARAM_IDCOMPETITION]);
		mysql_close();		
		$mv->setView('competition_fillresultform.tpl');
		return $mv;
	}
	
	/*function updatechamp_action() {
		$mv = new ModelAndView();
		$mv->addToModel(0, $this->_matchmodeldao->queryMatchByCompet($_GET[GLOBAL_PARAM_IDCOMPETITION]));
		mysql_close();		
		$mv->setView('competition_fillresultform.tpl');
		return $mv;
	}*/
	
	function genchamp_action() {
		$mv = new ModelAndView();
		$mv->addToModel(1, $this->_competitionmodeldao->findAllChamp());

		if(isset($_POST[GLOBAL_PARAM_IDCOMPETITION])) {
                        
			$mv->addToModel(0, $this->_matchmodeldao->queryMatchByCompet($_POST[GLOBAL_PARAM_IDCOMPETITION]));
		}
        $mv->addToModel(2, $_POST[GLOBAL_PARAM_IDCOMPETITION]);
		mysql_close();		
		$mv->setView('competition_generatechamp.tpl');
		return $mv;
	}
	
	function generatechamp_action() {
		$mv = new ModelAndView();
		$this->_competitionmodeldao->GenererChampionnat('17'); 
				
		return $this->diarychamp_action();
	}
	
	function diarychamp_action() {
		$mv = new ModelAndView();
		$mv->addToModel(1, $this->_competitionmodeldao->findAllChamp());

		if(isset($_POST[GLOBAL_PARAM_IDCOMPETITION])) {
                        
			$mv->addToModel(0, $this->_matchmodeldao->queryMatchByCompet($_POST[GLOBAL_PARAM_IDCOMPETITION]));
		}
        $mv->addToModel(2, $_POST[GLOBAL_PARAM_IDCOMPETITION]);
		mysql_close();		
		$mv->setView('competition_diarychamp.tpl');
		return $mv;
	}

	function savediarychamp_action() {
		$mv = new ModelAndView();
		$this->_matchmodeldao->savediarychamp($_POST); 
				
		return $this->diarychamp_action();
	}
	
	function preparechamp_action() {
		$mv = new ModelAndView();
		$mv->addToModel(1, $this->_competitionmodeldao->findAllChamp());
    	$mv->addToModel(0, $this->_teammodeldao->findAllTeam());
		$mv->addToModel(2, $_POST[GLOBAL_PARAM_IDCOMPETITION]);
		mysql_close();		
		$mv->setView('competition_preparechamp.tpl');
		return $mv;
	}
	
	function savepreparechamp_action() {
		$mv = new ModelAndView();
		$this->_competitionmodeldao->savepreparechamp($_POST); 
				
		return $this->diarychamp_action();
	}

	function scores_action() {
		//echo $_POST .' '.count($_POST).' '.$_POST["ID_MATCH"].' '.count($_POST["ID_MATCH"]).'</br>';
		for ($i=0; $i < count($_POST["ID_MATCH"]); $i++) 
		{ 
			//echo $_POST["ID_MATCH"][$i].'*'.$_POST["SCORE_TEAM_HOME"][$i].'*'.$_POST["SCORE_TEAM_VISITOR"][$i].'</br>';
			if ($_POST["SCORE_TEAM_HOME"][$i]!='' && $_POST["SCORE_TEAM_VISITOR"][$i]!='')
			{
				$match = new MatchModel(array("ID_MATCH" => $_POST["ID_MATCH"][$i],
											"SCORE_TEAM_HOME" => $_POST["SCORE_TEAM_HOME"][$i],
											"SCORE_TEAM_VISITOR" => $_POST["SCORE_TEAM_VISITOR"][$i],
											"ID_TEAM_HOME" => $_POST["ID_TEAM_HOME"][$i],
											"ID_TEAM_VISITOR" => $_POST["ID_TEAM_VISITOR"][$i]));
				//echo $match->getIdMatch().'</br>';
				$this->_matchmodeldao->UpdateMatchChampionnat($match);
			}
		}
		
		return $this->fillresultform_action();
	}
        
    function matchlist_action() {
		$mv = new ModelAndView();
		$mv->addToModel(0, $this->_competitionmodeldao->findCompetitionForCurrentSeason());
		$mv->addToModel(3, $this->_teammodeldao->findAllTeamManaged());

		
		$id_team=$_POST['id_team'];		
		$id_comp=$_POST['id_competition'];
		
		
		if($id_team!='') {
			$mv->addToModel(1, $this->_matchmodeldao->queryMatchByTeamAndCompetForCurrentSeason($id_team,$id_comp));
			if($id_comp!='') $mv->addToModel(2, $id_comp);
			else $mv->addToModel(2, 'ALL');
			$mv->addToModel(4, $_POST['id_team']);
		}
		else if($id_comp!='') {
			$mv->addToModel(1, $this->_matchmodeldao->queryMatchByCompet($id_comp));
			$mv->addToModel(2, $id_comp);
		} 
		else {
		   $mv->addToModel(1, $this->_matchmodeldao->queryAllMatchManagedLong());
		   $mv->addToModel(2, 'ALL');
		}  
                
		mysql_close();		
		$mv->setView('competition_matchlist.tpl');
		return $mv;
	}

    function ranking_action() {
		$mv = new ModelAndView();

		/* Old code removed by Z-Dan */
		/*
		$mv->addToModel(0, $this->_competitionmodeldao->findChampForCurrentSeason());

		if(isset($_POST[GLOBAL_PARAM_IDCOMPETITION])) 
		{
           $mv->addToModel(1, $this->_rankingmodeldao->queryRankingByCompet($_POST[GLOBAL_PARAM_IDCOMPETITION]));
		}
        $mv->addToModel(2, $_POST[GLOBAL_PARAM_IDCOMPETITION]);
		*/
		
        /* Nouveau code inserted by Z-Dan */
		$competitions = $this->_competitionmodeldao->findChampForCurrentSeason();
		foreach ($competitions as $key => $value) {
			$classements[$value] = $this->_rankingmodeldao->queryRankingByCompet($key);
		}
		$mv->addToModel(0, $classements);

		mysql_close();		

		$mv->setView('competition_ranking.tpl');
		return $mv;
	}
}

?>
